package com.sktlab.bizconfmobile.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import com.sktlab.bizconfmobile.R;
import com.sktlab.bizconfmobile.fragment.CalendarFragment;
import com.sktlab.bizconfmobile.fragment.DummyTabContent;
import com.sktlab.bizconfmobile.fragment.HistoryFragment;
import com.sktlab.bizconfmobile.fragment.HomeFragment;
import com.sktlab.bizconfmobile.fragment.ServiceFragment;
import com.sktlab.bizconfmobile.fragment.SettingFragment;
import com.sktlab.bizconfmobile.model.Constant;
import com.sktlab.bizconfmobile.util.Util;

/**
 * @Description:
 * 
 * @Author wenjuan.li
 * 
 * @date 2013-07-23
 * 
 * @version V1.0
 */

public class MainActivity extends FragmentActivity {
	
	public static final String TAG = "MainActivity";
	private TabHost tabHost;
	private TabWidget tabWidget;
	private HomeFragment homeFragment;
	private CalendarFragment calendarFragment;
	private HistoryFragment historyFragment;
	private ServiceFragment serviceFragment;
	private SettingFragment settingFragment;
	private android.support.v4.app.FragmentTransaction ft;

	private RelativeLayout tabIndicatorHome;
	private RelativeLayout tabIndicatorCalendar;
	private RelativeLayout tabIndicatorHistory;
	private RelativeLayout tabIndicatorService;
	private RelativeLayout tabIndicatorSetting;

	public final int TAB_HOME = 0;
	public final int TAB_CALENDAR = 1;
	public final int TAB_HISTORY = 2;
	public final int TAB_SERVICE = 3;
	public final int TAB_SETTING = 4;
	
	private TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {
		@Override
		public void onTabChanged(String tabId) {

			detecthAllFragment();
			
			int currentTabId = tabHost.getCurrentTab();
			
			switch(currentTabId) {

			case TAB_HOME:
				addFragmentHome();
				break;
			case TAB_CALENDAR:
				addFragmentCalendar();
				break;
			case TAB_HISTORY:
				addFragmentHistory();
				break;
			case TAB_SERVICE:
				addFragmentService();
				break;
			case TAB_SETTING:
				addFragmentSetting();
				break;
			default:
				addFragmentHome();
				break;
			}
			
			ft.commit();					
		}

	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		
		initTabHost();	
		
		//test code
		//String rcv = "336~0~BV.B.ACL~1~1~1~5~333~13661305367~61305367~~~0~1~2FC~15001377254~01377254~~~0~1~300~2016177387~16177387~~~0~1~2F4~8968454624~68454624~~~0~1~32E~15110021861~10021861~~~0~1";
		//MinaACLMsg msg = new MinaACLMsg(rcv);		
	}
	
	@Override
	public void onAttachFragment(Fragment fragment) {
		super.onAttachFragment(fragment);
		
		//Util.BIZ_CONF_DEBUG(TAG, "onAttachFragment called");
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		//To check whether the application's memory had been recycled
		if (Util.isMemoryRecyled()) {
			
			Util.BIZ_CONF_DEBUG(TAG, "memory recyled");
			
			ActivityBuffer.getInstance().clear();
			Util.startActivity(this, WelcomeActivity.class);	
			
			//finish this activity to restart it
			this.finish();
		}
		
		//Util.BIZ_CONF_DEBUG(TAG, "memory not cleared");
	}

	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		//reset the app's all data
		AppClass.getInstance().reset();
	}

	private void detecthAllFragment() {
		
		android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
		homeFragment = (HomeFragment) fm
				.findFragmentByTag(Constant.TAB_TAG_HOME);
		calendarFragment = (CalendarFragment) fm
				.findFragmentByTag(Constant.TAB_TAG_CALENDAR);
		
		historyFragment = (HistoryFragment)fm
				.findFragmentByTag(Constant.TAB_TAG_HISTORY);
		serviceFragment = (ServiceFragment) fm
				.findFragmentByTag(Constant.TAB_TAG_SERVICE);
		settingFragment = (SettingFragment) fm
				.findFragmentByTag(Constant.TAB_TAG_SETTING);
		ft = fm.beginTransaction();
		
		/** 如果存在Detaches掉 */
		if (homeFragment != null)
			ft.detach(homeFragment);

		/** 如果存在Detaches掉 */
		if (calendarFragment != null)
			ft.detach(calendarFragment);
		
		/** 如果存在Detaches掉 */
		if (historyFragment != null)
			ft.detach(historyFragment);
		
		/** 如果存在Detaches掉 */
		if (serviceFragment != null)
			ft.detach(serviceFragment);

		/** 如果存在Detaches掉 */
		if (settingFragment != null)
			ft.detach(settingFragment);
	}
	
	public void addFragmentHome() {

		if (homeFragment == null) {
			
			ft.add(R.id.realtabcontent, new HomeFragment(),
					Constant.TAB_TAG_HOME);
		} else {
			ft.attach(homeFragment);
		}
	}

	public void addFragmentCalendar() {

		if (calendarFragment == null) {
			ft.add(R.id.realtabcontent, new CalendarFragment(this),
					Constant.TAB_TAG_CALENDAR);
		} else {
			ft.attach(calendarFragment);
		}
	}
	
	public void addFragmentHistory() {

		if (historyFragment == null) {
			ft.add(R.id.realtabcontent, new HistoryFragment(),
					Constant.TAB_TAG_HISTORY);
		} else {
			ft.attach(historyFragment);
		}
	}
	
	public void addFragmentService() {

		if (serviceFragment == null) {
			ft.add(R.id.realtabcontent, new ServiceFragment(),
					Constant.TAB_TAG_SERVICE);
		} else {
			ft.attach(serviceFragment);
		}
	}

	public void addFragmentSetting() {

		if (settingFragment == null) {
			ft.add(R.id.realtabcontent, new SettingFragment(),
					Constant.TAB_TAG_SETTING);
		} else {
			ft.attach(settingFragment);
		}
	}
	
	public RelativeLayout generateTabIndicator(TabWidget tw, int imgId, int strId) {
		
		RelativeLayout indicator = (RelativeLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab1 = (TextView) indicator.getChildAt(1);
		ImageView ivTab1 = (ImageView) indicator.getChildAt(0);
		ivTab1.setBackgroundResource(imgId);
		tvTab1.setText(strId);
		
		return indicator;
	}
	
	public void initTabHost() {

		tabHost = (TabHost) findViewById(android.R.id.tabhost);
		tabWidget = (TabWidget) findViewById(android.R.id.tabs);
		
		LinearLayout layout = (LinearLayout) tabHost.getChildAt(0);
		TabWidget tw = (TabWidget) layout.getChildAt(1);

		tabIndicatorHome = generateTabIndicator(tw,R.drawable.selector_mood_home, R.string.main_tab_home);	
		tabIndicatorCalendar = generateTabIndicator(tw,R.drawable.selector_mood_calendar, R.string.main_tab_calendar);	
		tabIndicatorHistory = generateTabIndicator(tw,R.drawable.selector_mood_history, R.string.main_tab_history);	
		tabIndicatorService = generateTabIndicator(tw,R.drawable.selector_mood_service, R.string.main_tab_service);
		tabIndicatorSetting = generateTabIndicator(tw,R.drawable.selector_mood_setting, R.string.main_tab_setting);
		
		tabHost.setup();

		tabHost.setOnTabChangedListener(tabChangeListener);
		initTab();
		tabHost.setCurrentTab(0);
	}

	public void addTabSpec(String tag, RelativeLayout indicator) {
		
		TabHost.TabSpec tSpecHome = tabHost.newTabSpec(tag);
		tSpecHome.setIndicator(indicator);
		tSpecHome.setContent(new DummyTabContent(getBaseContext()));
		tabHost.addTab(tSpecHome);
	}
	
	public void initTab() {
		addTabSpec(Constant.TAB_TAG_HOME, tabIndicatorHome);
		addTabSpec(Constant.TAB_TAG_CALENDAR, tabIndicatorCalendar);
		addTabSpec(Constant.TAB_TAG_HISTORY, tabIndicatorHistory);
		addTabSpec(Constant.TAB_TAG_SERVICE, tabIndicatorService);
		addTabSpec(Constant.TAB_TAG_SETTING, tabIndicatorSetting);
	}
}
